package cn.dyp.com.automaticcontrolserver.action;


import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Element;

import android.content.Context;

import cn.dyp.com.automaticcontrolserver.ServerApp;
import cn.dyp.com.automaticcontrolserver.util.Util;

/**
 * @author zongbingwu 设置所有传感器阀值的请求处理类
 */
public class SetConfigAction extends BaseAction {
	// action name
	public static final String TAG = "setConfig";

	private Context context;

	public SetConfigAction(Context context) {
		this.context = context;
	}

	@Override
	protected String jasonPorcess(String param) {
		JSONObject jsonResponse = new JSONObject();
		try {
			// 处理设置告警范围请求
			ServerApp app = (ServerApp) context;
			JSONObject jsonRequest = new JSONObject(param);
			// 空气温度最小值
			if (jsonRequest.has("minAirTemperature")) {
				app.setMinAirTemperature(jsonRequest
						.getInt("minAirTemperature"));
				logObj = "air humiture";
			}
			// 空气温度最大值
			if (jsonRequest.has("maxAirTemperature")) {
				app.setMaxAirTemperature(jsonRequest
						.getInt("maxAirTemperature"));
				logObj = "air humiture";
			}
			// 空气温度最小值
			if (jsonRequest.has("minAirHumidity")) {
				app.setMinAirHumidity(jsonRequest.getInt("minAirHumidity"));
				logObj = "air humiture";
			}
			// 空气湿度最大值
			if (jsonRequest.has("maxAirHumidity")) {
				app.setMaxAirHumidity(jsonRequest.getInt("maxAirHumidity"));
				logObj = "air humiture";
			}

			// 土壤温度最小值
			if (jsonRequest.has("minSoilTemperature")) {
				app.setMinEarthTemperature(jsonRequest
						.getInt("minSoilTemperature"));
				logObj = "soil humiture";
			}
			// 土壤温度最大值
			if (jsonRequest.has("maxSoilTemperature")) {
				app.setMaxEarthTemperature(jsonRequest
						.getInt("maxSoilTemperature"));
				logObj = "soil humiture";
			}
			// 土壤湿度最小值
			if (jsonRequest.has("minSoilHumidity")) {
				app.setMinEarthHumidity(jsonRequest.getInt("minSoilHumidity"));
				logObj = "soil humiture";
			}
			// 土壤湿度最大值
			if (jsonRequest.has("maxSoilHumidity")) {
				app.setMaxEarthHumidity(jsonRequest.getInt("maxSoilHumidity"));
				logObj = "soil humiture";
			}

			// 灯光强度最小值
			if (jsonRequest.has("minLight")) {
				app.setMinLight(jsonRequest.getInt("minLight"));
				logObj = "light";
			}
			// 灯光强度最大值
			if (jsonRequest.has("maxLight")) {
				app.setMaxLight(jsonRequest.getInt("maxLight"));
				logObj = "light";
			}
			// CO2浓度最小值
			if (jsonRequest.has("minCo2")) {
				app.setMinCo2(jsonRequest.getInt("minCo2"));
				logObj = "CO2";
			}
			// CO2浓度最大值
			if (jsonRequest.has("maxCo2")) {
				app.setMaxCo2(jsonRequest.getInt("maxCo2"));
				logObj = "CO2";
			}

			// PM2.5浓度最大值
			if (jsonRequest.has("maxPM25")) {
				app.setMaxPm25(jsonRequest.getInt("maxPM25"));
				logObj = "PM25";
			}
			// 警告控制，0表示手动控制，1表示自动控制
			if (jsonRequest.has("controlAuto")) {
				int isAuto = jsonRequest.getInt("controlAuto");
				app.setControlAuto(isAuto);
				logObj = "Auto control";
				if (isAuto > 0) {
					logMsg = "Set auto control module";
				} else {
					logMsg = "Set manual operation module";
				}
			}
			// 返回结果
			jsonResponse.put("result", "ok");
			if (logMsg.equals("")) {
				logMsg += "Set threshold value for " + logObj;
			}
			return jsonResponse.toString();
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	protected String soapPorcess(String param) {
		Element soapBodyEle = Util.getSoapBodyElement(param);
		String ret = "failed";
		ServerApp app = (ServerApp) context;
		if (soapBodyEle != null) {
			try {
				// 空气温度最小值
				Element minAirT = (Element) soapBodyEle.getElementsByTagName(
						"minAirTemperature").item(0);
				if (minAirT != null) {
					app.setMinAirTemperature(Integer.parseInt(minAirT
							.getFirstChild().getNodeValue()));
					logObj = "air humiture";
				}
				// 空气温度最大值
				Element maxAirT = (Element) soapBodyEle.getElementsByTagName(
						"maxAirTemperature").item(0);
				if (maxAirT != null) {
					app.setMaxAirTemperature(Integer.parseInt(maxAirT
							.getFirstChild().getNodeValue()));
					logObj = "air humiture";
				}
				// 空气湿度最小值
				Element minAirH = (Element) soapBodyEle.getElementsByTagName(
						"minAirHumidity").item(0);
				if (minAirH != null) {
					app.setMinAirHumidity(Integer.parseInt(minAirH
							.getFirstChild().getNodeValue()));
					logObj = "air humiture";
				}
				// 空气湿度最大值
				Element maxAirH = (Element) soapBodyEle.getElementsByTagName(
						"maxAirHumidity").item(0);
				if (maxAirH != null) {
					app.setMaxAirHumidity(Integer.parseInt(maxAirH
							.getFirstChild().getNodeValue()));
					logObj = "air humiture";
				}
				// 土壤温度最小值
				Element minSoilT = (Element) soapBodyEle.getElementsByTagName(
						"minSoilTemperature").item(0);
				if (minSoilT != null) {
					app.setMinEarthTemperature(Integer.parseInt(minSoilT
							.getFirstChild().getNodeValue()));
					logObj = "soil humiture";
				}
				// 土壤温度最大值
				Element maxSoilT = (Element) soapBodyEle.getElementsByTagName(
						"maxSoilTemperature").item(0);
				if (maxSoilT != null) {
					app.setMaxEarthTemperature(Integer.parseInt(maxSoilT
							.getFirstChild().getNodeValue()));
					logObj = "soil humiture";
				}
				// 土壤湿度最小值
				Element minSoilH = (Element) soapBodyEle.getElementsByTagName(
						"minSoilHumidity").item(0);
				if (minSoilH != null) {
					app.setMinEarthHumidity(Integer.parseInt(minSoilH
							.getFirstChild().getNodeValue()));
					logObj = "soil humiture";
				}
				// 土壤湿度最大值
				Element maxSoilH = (Element) soapBodyEle.getElementsByTagName(
						"maxSoilHumidity").item(0);
				if (maxSoilH != null) {
					app.setMaxEarthHumidity(Integer.parseInt(maxSoilH
							.getFirstChild().getNodeValue()));
					logObj = "soil humiture";
				}
				// 灯光强度最小值
				Element minLihgt = (Element) soapBodyEle.getElementsByTagName(
						"minLight").item(0);
				if (minLihgt != null) {
					app.setMinLight(Integer.parseInt(minLihgt.getFirstChild()
							.getNodeValue()));
					logObj = "light";
				}
				// 灯光强度最大值
				Element maxLihgt = (Element) soapBodyEle.getElementsByTagName(
						"maxLight").item(0);
				if (maxLihgt != null) {
					app.setMaxLight(Integer.parseInt(maxLihgt.getFirstChild()
							.getNodeValue()));
					logObj = "light";
				}
				// CO2浓度最小值
				Element minCo2 = (Element) soapBodyEle.getElementsByTagName(
						"minCo2").item(0);
				if (minCo2 != null) {
					app.setMinCo2(Integer.parseInt(minCo2.getFirstChild()
							.getNodeValue()));
					logObj = "CO2";
				}
				// CO2浓度最大值
				Element maxCo2 = (Element) soapBodyEle.getElementsByTagName(
						"maxCo2").item(0);
				if (maxCo2 != null) {
					app.setMaxCo2(Integer.parseInt(maxCo2.getFirstChild()
							.getNodeValue()));
					logObj = "CO2";
				}
				// PM2.5浓度最大值
				Element maxPm25 = (Element) soapBodyEle.getElementsByTagName(
						"maxPM25").item(0);
				if (maxPm25 != null) {
					app.setMaxPm25(Integer.parseInt(maxPm25.getFirstChild()
							.getNodeValue()));
					logObj = "PM25";
				}
				// 警告控制，0表示手动控制，1表示自动控制
				Element controlAuto = (Element) soapBodyEle
						.getElementsByTagName("controlAuto").item(0);
				if (controlAuto != null) {
					int isAuto = Integer.parseInt(controlAuto.getFirstChild()
							.getNodeValue());
					app.setControlAuto(isAuto);
					logObj = "Auto control";
					if (isAuto > 0) {
						logMsg = "Set auto control module";
					} else {
						logMsg = "Set manual operation module";
					}
				}
				ret = "ok";
				if (logMsg.equals("")) {
					logMsg += "Set threshold value for " + logObj;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		// 返回结果
		return Util.getSoapXml(Util.getXmlElementStr("result", ret));
	}

}
